package ar.com.drconta.ganancias.editors.example;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent;
import org.eclipse.jface.viewers.ColumnViewerEditorActivationStrategy;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;

import ar.com.drconta.ganancias.editors.example.model.mock.MockUtil;
import ar.com.drconta.ganancias.editors.example.modelVO.City;
import ar.com.drconta.ganancias.views.vogella.Todo;

public class View extends ViewPart {
	public static final String ID = "ar.com.drconta.ganancias.editors.example.view";
	private TreeViewer m_treeViewer;

	class ViewLabelProvider extends LabelProvider implements ILabelProvider {
		public String getColumnText(Object obj, int index) {
			return getText(obj);
		}

		public Image getColumnImage(Object obj, int index) {
			return getImage(obj);
		}

		public Image getImage(Object obj) {
			return PlatformUI.getWorkbench().getSharedImages()
					.getImage(ISharedImages.IMG_OBJ_ELEMENT);
		}
	}

	public void createPartControl(Composite parent) {
		parent.setLayout(new FillLayout());

		Tree addressTree = new Tree(parent, SWT.H_SCROLL | SWT.V_SCROLL
				| SWT.BORDER);
		addressTree.setHeaderVisible(true);
		m_treeViewer = new TreeViewer(addressTree);

		// m_treeViewer.addSelectionChangedListener(new
		// ISelectionChangedListener() {
		// @Override
		// public void selectionChanged(SelectionChangedEvent event){
		// System.out.println(event.getSelection());
		// MyEditor.openEditor(((ITreeSelection)event.getSelection()).getFirstElement());
		// }
		// });
		/***/

		m_treeViewer.setColumnProperties(new String[] { "detalle", "enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre"});
//		m_treeViewer.setCellModifier(new ICellModifier() {
//
//			@Override
//			public void modify(Object element, String property, Object value) {
//				// TODO Auto-generated method stub
//
//			}
//
//			@Override
//			public Object getValue(Object element, String property) {
//				// TODO Auto-generated method stub
//				return null;
//			}
//
//			@Override
//			public boolean canModify(Object element, String property) {
//				// TODO Auto-generated method stub
//				return true;
//			}
//		});

		/***/
		addressTree.setLinesVisible(true);

		addressTree.setLayoutData(new GridData(GridData.FILL, GridData.FILL,
				true, true));

		addressTree.setRedraw(true);
		addressTree.setVisible(true);

		TreeColumn column1 = new TreeColumn(addressTree, SWT.LEFT);
		column1.setAlignment(SWT.LEFT);
		column1.setText("Detalle");
		column1.setWidth(300);
		TreeColumn columnJunary = new TreeColumn(addressTree, SWT.RIGHT);
		columnJunary.setAlignment(SWT.LEFT);
		columnJunary.setText("Enero");
		columnJunary.setWidth(85);
		TreeColumn columnFebrary = new TreeColumn(addressTree, SWT.RIGHT);
		columnFebrary.setAlignment(SWT.LEFT);
		columnFebrary.setText("Febrero");
		columnFebrary.setWidth(85);
		TreeColumn columnMarch = new TreeColumn(addressTree, SWT.RIGHT);
		columnMarch.setAlignment(SWT.LEFT);
		columnMarch.setText("Marzo");
		columnMarch.setWidth(85);
		TreeColumn columnAphil = new TreeColumn(addressTree, SWT.RIGHT);
		columnAphil.setAlignment(SWT.LEFT);
		columnAphil.setText("Abril");
		columnAphil.setWidth(85);
		TreeColumn columnMay = new TreeColumn(addressTree, SWT.RIGHT);
		columnMay.setAlignment(SWT.LEFT);
		columnMay.setText("Mayo");
		columnMay.setWidth(85);
		TreeColumn columnJune = new TreeColumn(addressTree, SWT.RIGHT);
		columnJune.setAlignment(SWT.LEFT);
		columnJune.setText("Junio");
		columnJune.setWidth(85);
		TreeColumn columnJuly = new TreeColumn(addressTree, SWT.RIGHT);
		columnJuly.setAlignment(SWT.LEFT);
		columnJuly.setText("Julio");
		columnJuly.setWidth(85);
		TreeColumn columnAugust = new TreeColumn(addressTree, SWT.RIGHT);
		columnAugust.setAlignment(SWT.LEFT);
		columnAugust.setText("Agosto");
		columnAugust.setWidth(85);
		TreeColumn columnSeptember = new TreeColumn(addressTree, SWT.RIGHT);
		columnSeptember.setAlignment(SWT.LEFT);
		columnSeptember.setText("Septiembre");
		columnSeptember.setWidth(85);
		TreeColumn columnOctober = new TreeColumn(addressTree, SWT.RIGHT);
		columnOctober.setAlignment(SWT.LEFT);
		columnOctober.setText("Octubre");
		columnOctober.setWidth(85);
		TreeColumn columnNovember = new TreeColumn(addressTree, SWT.RIGHT);
		columnNovember.setAlignment(SWT.LEFT);
		columnNovember.setText("Noviembre");
		columnNovember.setWidth(85);
		TreeColumn columnDecember = new TreeColumn(addressTree, SWT.RIGHT);
		columnDecember.setAlignment(SWT.LEFT);
		columnDecember.setText("Diciembre");
		columnDecember.setWidth(85);
		
		m_treeViewer.setContentProvider(new FoldersContentProvider(this));
		m_treeViewer.setLabelProvider(new TableLabelProvider(this));
		
//		List<City> cities = new ArrayList<City>();
//		cities.add(new City(this));
//		m_treeViewer.setInput(cities);
		
		m_treeViewer.setInput(MockUtil.getPersonalFolder1());
		m_treeViewer.expandAll();
		
//		m_treeViewer.get
		CellEditor[] editors = new CellEditor[13];
	    editors[0] = new TextCellEditor(m_treeViewer.getTree());
	    editors[1] = new TextCellEditor(m_treeViewer.getTree());
	    editors[2] = new TextCellEditor(m_treeViewer.getTree());
	    editors[3] = new TextCellEditor(m_treeViewer.getTree());
	    editors[4] = new TextCellEditor(m_treeViewer.getTree());
	    editors[5] = new TextCellEditor(m_treeViewer.getTree());
	    editors[6] = new TextCellEditor(m_treeViewer.getTree());
	    editors[7] = new TextCellEditor(m_treeViewer.getTree());	    
	    editors[8] = new TextCellEditor(m_treeViewer.getTree());
	    editors[9] = new TextCellEditor(m_treeViewer.getTree());
	    editors[10] = new TextCellEditor(m_treeViewer.getTree());
	    editors[11] = new TextCellEditor(m_treeViewer.getTree());
	    editors[12] = new TextCellEditor(m_treeViewer.getTree());
	    
		m_treeViewer.setCellEditors(editors);
		m_treeViewer.setCellModifier(new ItemCellModifier(m_treeViewer));
		
		
		//
		// // add a doubleclicklistener
		// m_treeViewer.addDoubleClickListener(new IDoubleClickListener() {
		//
		// @Override
		// public void doubleClick(DoubleClickEvent event) {
		// TreeViewer viewer = (TreeViewer) event.getViewer();
		// IStructuredSelection thisSelection = (IStructuredSelection) event
		// .getSelection();
		// Object selectedNode = thisSelection.getFirstElement();
		// viewer.setExpandedState(selectedNode,
		// !viewer.getExpandedState(selectedNode));
		// }
		// });

		// m_treeViewer.getTree().addKeyListener(new KeyAdapter() {
		//
		// @Override
		// public void keyReleased(final KeyEvent e) {
		// if (e.keyCode == SWT.DEL) {
		// final IStructuredSelection selection = (IStructuredSelection)
		// m_treeViewer
		// .getSelection();
		// if (selection.getFirstElement() instanceof Todo) {
		// Todo o = (Todo) selection.getFirstElement();
		// // TODO Delete the selected element from the model
		// }
		//
		// }
		// }
		// });

		// column5.addListener(SWT.EraseItem, new Listener()
		// {
		// public void handleEvent(Event event)
		// {
		// String text = ((TreeItem)event.item).getText(event.index);
		// Point size = event.gc.textExtent(text);
		// TreeColumn column = ((Tree)event.widget).getColumn(event.index);
		// int columnWidth = column.getWidth() - 10; /* magic number alert - the
		// cells have some padding - must be a way of determining this... */
		// if(size.x > columnWidth)
		// {
		// event.detail &= ~SWT.FOREGROUND;
		// }
		// }
		// });
		//
		// column4.addListener(SWT.PaintItem, new Listener() {
		//
		// @Override
		// public void handleEvent(Event event) {
		// // TODO Auto-generated method stub
		//
		// }
		// });

	}

	public void setFocus() {
		m_treeViewer.getControl().setFocus();
	}
}